//
//  UserView.m
//  LoveOfPet
//
//  Created by 芦彬 on 15/11/19.
//  Copyright © 2015年 孔维. All rights reserved.
//

#import "DetailOfUserView.h"
#import "LuBDefine.h"

#import "SDWebImageManager.h"
#import "UIImageView+WebCache.h"

@implementation DetailOfUserView

- (void)dealloc{
    
    [_detailOfCircle release];
    [_userPhoto_desLabel release];
    [_userAvatarImageView release];
    [_userDisnameLabel release];
    [_userSexLabel release];
    [_userTimeLabel release];
    [_userZanlabel release];
    [super dealloc];
}

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self createDetailOfUserView];
    }
    return self;
}

- (void)createDetailOfUserView{
    //---用户---
    self.userAvatarImageView = [[UIImageView alloc]init];
    self.userAvatarImageView.backgroundColor = [UIColor redColor];
    [self addSubview:_userAvatarImageView];
    [_userAvatarImageView release];
    
    self.userDisnameLabel = [[UILabel alloc]init];
//    self.userDisnameLabel.backgroundColor = [UIColor orangeColor];
    [self addSubview:_userDisnameLabel];
    [_userDisnameLabel release];
    
    self.userTimeLabel = [[UILabel alloc]init];
//    self.userTimeLabel.backgroundColor = [UIColor magentaColor];
    [self addSubview:_userTimeLabel];
    [_userTimeLabel release];
    
    self.userSexLabel = [[UILabel alloc]init];
//    self.userSexLabel.backgroundColor = [UIColor grayColor];
    [self addSubview:_userSexLabel];
    [_userSexLabel release];
    
    self.userZanlabel = [[UILabel alloc]init];
//    self.userZanlabel.backgroundColor = [UIColor grayColor];
    [self addSubview:_userZanlabel];
    [_userZanlabel release];
    
    self.userPhoto_desLabel = [[UILabel alloc]init];
//    self.userPhoto_desLabel.backgroundColor = [UIColor greenColor];
    [self addSubview:_userPhoto_desLabel];
    [_userPhoto_desLabel release];
}

//用户
- (void)setDetailOfCircle:(DetailOfCircle *)detailOfCircle{
    if (_detailOfCircle != detailOfCircle) {
        [_detailOfCircle release];
        _detailOfCircle = [detailOfCircle retain];
    }
    NSURL *userAvatarUrl = [NSURL URLWithString:detailOfCircle.avatar];
    [self.userAvatarImageView sd_setImageWithURL:userAvatarUrl placeholderImage:[UIImage imageNamed:@"zhanweitu.jpg"]];
    
    self.userDisnameLabel.text = detailOfCircle.disname;
    self.userPhoto_desLabel.text = detailOfCircle.photo_des;
    if ([detailOfCircle.sex isEqualToString:@"1"]) {
        self.userSexLabel.text = @"🎩公";
    }else {
        self.userSexLabel.text = @"🎀母";
    }
    self.userZanlabel.text = [NSString stringWithFormat:@"👍%@",detailOfCircle.zan];
    self.userTimeLabel.text = detailOfCircle.time;
    _autoHeight = [DetailOfUserView heightOfLabel:self.userPhoto_desLabel.text andTextFont:[UIFont systemFontOfSize:13] withLabelWidths:WIDTH - 40];
  
}

+ (CGFloat)heightOfLabel:(NSString *)text andTextFont:(UIFont *)font withLabelWidths:(CGFloat)widths{
    CGSize size = CGSizeMake(widths, MAXFLOAT);
    NSDictionary *attributeDic = [NSDictionary dictionaryWithObject:font forKey:NSFontAttributeName];
    CGRect frame = [text boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:attributeDic context:nil];
    return frame.size.height;
}

- (void)layoutSubviews{
    [super layoutSubviews];
    self.userAvatarImageView.frame = CGRectMake(10, 10, WIDTH / 6, WIDTH / 6);
    self.userAvatarImageView.layer.shadowColor = [[UIColor blackColor] CGColor];
    self.userAvatarImageView.layer.shadowOffset = CGSizeMake(5, 5);
    self.userAvatarImageView.layer.shadowOpacity = 1;
    self.userAvatarImageView.layer.borderColor = [[UIColor whiteColor] CGColor];
    self.userAvatarImageView.layer.borderWidth = 3;
    
    self.userDisnameLabel.frame = CGRectMake(10 + self.userAvatarImageView.frame.size.width + 20, 10, WIDTH / 4, 30);
    self.userDisnameLabel.textColor = [UIColor redColor];
    self.userDisnameLabel.font = [UIFont systemFontOfSize:15];
    
    self.userTimeLabel.frame = CGRectMake(WIDTH - WIDTH / 4 - 10, 10, WIDTH / 4, 30);
    self.userTimeLabel.textAlignment = NSTextAlignmentRight;
    self.userTimeLabel.textColor = [UIColor grayColor];
    self.userTimeLabel.font = [UIFont systemFontOfSize:13];
    
    self.userSexLabel.frame = CGRectMake(10 + self.userAvatarImageView.frame.size.width + 20, 10 + self.userDisnameLabel.frame.size.height, WIDTH / 4, 30);
    self.userSexLabel.textColor = [UIColor grayColor];
    self.userSexLabel.font = [UIFont systemFontOfSize:13];
    
    self.userZanlabel.frame = CGRectMake(WIDTH - WIDTH / 4, 10 + self.userDisnameLabel.frame.size.height, WIDTH / 4 - 10, 30);
    self.userZanlabel.textAlignment = NSTextAlignmentRight;
    self.userZanlabel.textColor = [UIColor redColor];
    self.userZanlabel.font = [UIFont systemFontOfSize:13];
    
    self.userPhoto_desLabel.frame = CGRectMake(20 , 10 + self.userAvatarImageView.frame.size.height + 10, WIDTH - 30, self.autoHeight);
    self.userPhoto_desLabel.textColor = [UIColor grayColor];
    self.userPhoto_desLabel.font = [UIFont systemFontOfSize:13];
    self.userPhoto_desLabel.numberOfLines = 0;
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.target performSelector:self.action withObject:self];
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
